package com.eclab.service.impl;

import com.eclab.dao.AccountDao;
import com.eclab.entity.Account;
import com.eclab.service.AccountService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Account)表服务实现类
 *
 * @author marin
 * @since 2020-04-23 09:46:32
 */
@Service
public class AccountServiceImpl implements AccountService {
    @Resource
    private AccountDao accountDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Account queryById(Integer id) {
        return accountDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Account> queryAllByLimit(int offset, int limit) {
        return accountDao.queryAllByLimit(offset, limit);
    }


    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public List<Account> findAll() {
        return accountDao.findAll();
    }

    /**
     * 新增数据
     *
     * @param account 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public Account insert(Account account) {
        accountDao.insert(account);
        return account;
    }

    /**
     * 修改数据
     *
     * @param account 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public Account update(Account account) {
        this.accountDao.update(account);
        return queryById(account.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @Transactional
    public String deleteById(Integer id) {
        boolean flag = accountDao.deleteById(id) > 0;
        if (flag) {
            return "删除成功";
        }
        return "删除失败";
    }

    @Override
    public Account findAccountByName(String name) {
        List<Account> accounts = accountDao.findAccountByName(name);
        if (accounts.size()>1){
            return accounts.get(0);
        }else{
            throw new RuntimeException("无此用户");
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public String transform(String source, String target, float money) {
        List<Account> sourceAccounts = accountDao.findAccountByName(source);
        List<Account> targetAccounts = accountDao.findAccountByName(target);
        if(sourceAccounts.size()>1 || sourceAccounts.size()==0){
            return "用户不存在";
        }
        if(targetAccounts.size()>1 || sourceAccounts.size()==0){
            return "转账目标用户不存在";
        }
        float transformMoney = (float)sourceAccounts.get(0).getMoney() - money;
          if(transformMoney < 0){
              return "转账失败";
          }
              sourceAccounts.get(0).setMoney(transformMoney);
              targetAccounts.get(0).setMoney((float)targetAccounts.get(0).getMoney()+money);
              accountDao.update(sourceAccounts.get(0));
              int i =1/0;
              accountDao.update(targetAccounts.get(0));
              return "转账成功！";
    }

}